﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PatientInformationExtract.MainInterface.Models.XMLCS;
using Microsoft.Office.Interop.Excel;
using System.Windows;
using PanGu;
using System.Xml.Xsl;
using PatientInformationExtract.Components;
using MircoSoftTanslator;

namespace PatientInformationExtract.MainInterface.Models
{
    public class MainInterfaceModel : ModelBase, IDisposable
    {
        private Microsoft.Office.Interop.Excel.Application _excelApp;

        private Translator _translator;
        public Translator Translator
        {
            get
            {
                if (this._translator == null)
                {
                    try
                    {
                        this._translator = new Translator("PatientInformationTranslate", "PXNQ/w54tUo1UqNoaT5AYNIWErv2twGYM3LMu5FnoLc=", new TimeSpan(0, 0, 10));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("连接MircoSoft Translate服务失败！Error message: " + ex.Message);
                    }
                }
                return this._translator;
            }
        }

        private CMLPDictHelper.CMLPDictHelper _cmlpDictHelper;
        public CMLPDictHelper.CMLPDictHelper CMLPDictHelper
        {
            get
            {
                if (this._cmlpDictHelper == null)
                {
                    try
                    {
                        this._cmlpDictHelper = new CMLPDictHelper.CMLPDictHelper();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("连接CMLP_DICT数据库失败！Error message:" + ex.Message);                   
                    }
                }
                return this._cmlpDictHelper;
            }
        }

        private bool _isInitialized = false;
        public bool IsInitialized { get { return this._isInitialized; } }

        private Workbooks _excelWBs;
        public Workbooks ExcelWorkBooks
        {
            get { return this._excelWBs; }
        }

        private TranslationLearningDictionary _translationLearningDict;
        public TranslationLearningDictionary TranslationLearningDict { get { return this._translationLearningDict; } }

        private XslCompiledTransform _xslt;
        public XslCompiledTransform Xslt { get { return this._xslt; } }

        private Segment _segment;
        public Segment PanGuSegment
        {
            get { return this._segment; }
        }

        private EventLog _eventLog;
        public EventLog EventLog
        {
            get { return this._eventLog; }
            set { this._eventLog = value; }
        }

        private List<string> _patientList;
        public List<string> PatientList
        {
            get { return this._patientList; }
            set { this._patientList = value; }
        }

        public MainInterfaceModel(ModelBase parentModel)
            : base(parentModel)
        {
            try
            {
                this._excelApp = new Microsoft.Office.Interop.Excel.Application();
                if (_excelApp == null)
                {
                    throw new Exception("打开Excel应用时发生错误！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开Excel应用时发生错误！Error message: " + ex.Message);
                this._isInitialized = false;
                return;
            }

            try
            {
                this._xslt = new XslCompiledTransform();
                this._xslt.Load(System.IO.Path.GetFullPath(@".\xml-pretty-print.xsl"));
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取xml-pretty-print.xsl失败！Error message: " + ex.Message);
                this._isInitialized = false;
                return;
            }

            try
            {
                Segment.Init(System.IO.Path.GetFullPath(@".\PanGu.xml"));
                this._segment = new Segment();
            }
            catch (Exception ex)
            {
                MessageBox.Show("盘古分词初始化失败！Error message: " + ex.Message);
                this._isInitialized = false;
                return;
            }

            try
            {
                this._translationLearningDict = new TranslationLearningDictionary(PanGu.Setting.PanGuSettings.Config.GetDictionaryPath() + "TranslationLearningDict.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取Translation Learning Dictionary失败！Error message: " + ex.Message);
                this._isInitialized = false;
                return;
            }

            this._patientList = new List<string>();
            this._excelWBs = _excelApp.Workbooks;
            this._eventLog = new PatientInformationExtract.MainInterface.Models.XMLCS.EventLog(_excelWBs);

            this._isInitialized = true;
        }

        public MainInterfaceModel()
            : this(null)
        {
        }

        public void Dispose()
        {
            try
            {
                this._excelWBs.Close();
                ReleaseCOM(_excelWBs);
                this._excelApp.Quit();
                ReleaseCOM(_excelApp);
            }
            catch (Exception ex)
            {
                MessageBox.Show("关闭Excel出错！Error message: " + ex.Message);
            }
        }

        private void ReleaseCOM(object pObj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pObj);
            }
            catch (Exception ex)
            {
                throw new Exception("释放资源时发生错误！Error message: " + ex.Message);
            }
            finally
            {
                pObj = null;
            }
        }

    }
}
